$(document).ready(function(){
	$.ajax({
		method: "get",
		url: getContextPath()+"/AddTask.oa",
		data: {cmd: 'taskList', time : new Date().getTime()}, 
		contentType: "text/html;charset=utf-8",
		dataType: "html",     
		success: function (msg) {
			$("#taskList").html(msg);
			getPage();
		}
	})
})

function getContextPath(){ 
	var pathName = document.location.pathname; 
	var index = pathName.substr(1).indexOf("/"); 
	var result = pathName.substr(0,index+1); 
	return result; 
}

$(function(){

	$("#pageSize").change(function(){
		$.ajax({
			method: "get",
			url: getContextPath()+"/AddTask.oa",
			data: {cmd: 'pS' , pageSize: this.value}, 
			contentType: "text/html;charset=utf-8",
			dataType: "html",
			success: function (msg) {
				$("#taskList").html(msg);
				getPage();
			}
		})
	})
	
	$("#keyWord").keyup(function(event){
		if(event.keyCode == 13){
			var key = $("#keyWord").val();
			if (key.length<2){
				alert("搜索关键字最少需要两个字符");
				return;
			}
			$.ajax({
				method: "get",
				url: getContextPath()+"/AddTask.oa",
				data: {cmd: 'kW', keyWord: encodeURI(key)}, 
				contentType: "text/html;charset=utf-8",
				dataType: "html",
				success: function (msg) {
					$("#taskList").html(msg);
					getPage();
				}
			})	
		}
	});
	
	$("#frontpage").click(function(){
		pageMove(-1024*1024);//首页
	})
	
	$("#formerPage").click(function(){
		pageMove(-1);
	})
	
	$("#nextPage").click(function(){
		console.log("nextPage");
		pageMove(1);
	})
	
	$("#finalpage").click(function(){
		pageMove(1024*1024);//去尾页
	})
	
	$("#secondpage").click(function(){
		pageMove(1);
	})
	$("#thirdpage").click(function(){
		pageMove(2);
	})
	$("#fourthpage").click(function(){
		pageMove(3);
	})
	
	$("#remove").click(function(){
		delUser(this.value);
	})
	
	function pageMove(move){
		$.ajax({
			method: "get",
			url: getContextPath()+"/AddTask.oa",
			data: {cmd: 'pM', move: move}, 
			contentType: "text/html;charset=utf-8",
			dataType: "html",
			success: function (msg) {
				$("#taskList").html(msg);
				getPage();
			}
		})		
	}
	
})

function getPage(){
	$.ajax({
		method: "get",
		url: getContextPath()+"/AddTask.oa",
		data: {cmd: 'getPage'}, 
		contentType: "text/html;charset=utf-8",
		dataType: "json",     
		success: function (result) {
			$("#currentPage").text(result.currentPage);
			$("#totalCount").text(result.totalCount);
			$("#totalPage").text(result.totalPage);
			
			$("#p1").text(result.currentPage);
				
			if (result.currentPage+1<=result.totalPage){
				$("#secondpage").css('display','block');
				$("#p2").text(result.currentPage+1);
			}else {
				$("#secondpage").css('display','none');
			}
				
			if (result.currentPage+2<=result.totalPage){
				$("#thirdpage").css('display','block');
				$("#p3").text(result.currentPage+2);
			}else {
				$("#thirdpage").css('display','none');
			}
			
			if (result.currentPage+3<=result.totalPage){
				$("#fourthpage").css('display','block');
				$("#p4").text(result.currentPage+3);
			}else {
				$("#fourthpage").css('display','none');
			}		
		}
	})
}